const sourceType = [['camera'], ['album'], ['camera', 'album']]
const imageType = [['image'], ['video'], ['mix']]
const sizeType = [['compressed'], ['original'], ['compressed', 'original']]
import utils from '../../../../util/util.js'
Page({
  onShareAppMessage() {
    return {
      title: '图片',
      path: 'packageAPI/pages/media/image/image'
    }
  },

  data: {
    title:'',
    showResult:false,
    canvasHeight: 400, // 初始高度
    theme: 'light',
    imageList: [],
    wxml: '', // 格式化后的 WXML 字符串
    style: {
      view: { backgroundColor: '#0000' },
      text: { fontSize: 16,color:'#fff' }
    },
    maxDuration:10,
    resultStr:'',
    width:0,
    height:0,
    cameraType:['后置摄像头','前置摄像头'],
    cameraTypeIndex:0,
    sourceTypeIndex: 2,
    sourceType: ['拍照', '相册', '拍照或相册'],
    imageType: ['只能拍摄图片或从相册选择图片', '只能拍摄视频或从相册选择视频', '可同时选择图片和视频'],
    imageTypeIndex:2,
    sizeTypeIndex: 2,
    sizeType: ['压缩', '原图', '压缩或原图'],

    countIndex: 8,
    count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    tempImage:""
  },
  sourceTypeChange(e) {
    this.setData({
      sourceTypeIndex: e.detail.value
    })
  },
  imageTypeChange(e){
    this.setData({
      imageTypeIndex: e.detail.value
    })
  },
  cameraTypeChange(e){
    this.setData({
      cameraTypeIndex: e.detail.value
    })
  },
  sizeTypeChange(e) {
    this.setData({
      sizeTypeIndex: e.detail.value
    })
  },
  countChange(e) {
    this.setData({
      countIndex: e.detail.value
    })
  },
  inputNum(e){
    console.log(e)
    if(parseInt(e.detail.value)>60){
      this.setData({
        maxDuration:60
      })
    }else if(parseInt(e.detail.value)<1){
      this.setData({
        maxDuration:1
      })
    }else{
      this.setData({
        maxDuration:parseInt(e.detail.value)
      })
    }
  },
  chooseImage() {
    const that = this
    wx.chooseMedia({
      count: this.data.count[this.data.countIndex],
      mediaType: imageType[this.data.imageTypeIndex],
      sizeType: sizeType[this.data.sizeTypeIndex],
      sourceType: sourceType[this.data.sourceTypeIndex],
      maxDuration: this.data.maxDuration,
      camera: ['back','front'][this.data.cameraTypeIndex],
      success(res) {
        that.setData({
          imageList:res.tempFiles,
          resultStr: that.jsonToHtml(res, 0)
        })
        that.setData({
          showResult:true,
          resultStr:utils.jsonToHtml(res,0)
        })
        console.log('选择的图片', JSON.stringify(res, null, 2));

        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
      }
    })
  },
 
  previewResult(){
    wx.canvasToTempFilePath({
      canvasId: 'jsonCanvas',
      success: (res) => {
        wx.previewImage({
          current:res.tempFilePath,
          urls: [res.tempFilePath]
        })
        this.setData({ imgPath: res.tempFilePath });
      }
    }, this);
  },
  previewImage(e) {
    const current = e.target.dataset.src

    wx.previewImage({
      current,
      urls: this.data.imageList
    })
  },
  onLoad(options) {
    this.setData({
      theme: wx.getSystemInfoSync().theme || 'light'
    })
    if(options&&options.zh){
      this.setData({
        title: options.zh
      })
    }
    if (wx.onThemeChange) {
      wx.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
  }
})
